package cevent.source.cloudcenter.business.controller.admin;

import cevent.source.cloudcenter.server.dto.MemberDto;
import cevent.source.cloudcenter.server.dto.ModulePicPathDto;
import cevent.source.cloudcenter.server.dto.PageDto;
import cevent.source.cloudcenter.server.dto.ResponseDataDto;
import cevent.source.cloudcenter.server.service.MemberService;
import cevent.source.cloudcenter.server.service.ShortMessageService;
import cevent.source.cloudcenter.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author cevent
 * @description 会员管理表控制层
 */
@RestController
@RequestMapping("/admin/member")
public class MemberController {
    private static final Logger LOG = LoggerFactory.getLogger(MemberController.class);
    //AOP日志环绕
    public static final String BUSINESS_NAME="模块管理 会员管理表";
    @Resource
    private MemberService memberService;
    @Resource
    private ShortMessageService shortMessageService;
    //1.dto-member获取
    @RequestMapping("/dtoList")
    public ResponseDataDto getDtoList() {
        ResponseDataDto responseData=new ResponseDataDto();
        responseData.setResponseData(memberService.memberDtoList());
        return responseData;
    }


    /*2.前端分页设置，后端pageInfo处理
    前端传参方式一：表单方式--》formData 前端：直接传对象   后端参数：(Dto对象)
    前端传参方式二：流的方式--》data{param1:value1,param2:vlaue2}  后端参数：(@RequestBody Dto dto对象)
     */
    @PostMapping("/pageList")
    public ResponseDataDto list(@RequestBody PageDto pageDto) {
        LOG.info("查看controller的pageDto：{}", pageDto);
        ResponseDataDto responseData = new ResponseDataDto();
        memberService.getMemberPageDtoList(pageDto);
        responseData.setResponseData(pageDto);
        return responseData;
    }

    //3.新增，流方式传参，加入@RequestBody
    @PostMapping("/save")
    public ResponseDataDto save(@RequestBody MemberDto memberDto) {
        LOG.info("传入的member DTO：{}", memberDto);

        ValidatorUtil.requiredLength(memberDto.getName(), "姓名", 1, 255);
        /*ValidatorUtil.requiredLength(memberDto.getNickName(), "昵称", 3, 255);
        ValidatorUtil.requiredLength(memberDto.getIcon(), "头像", 3, 500);
        ValidatorUtil.requiredLength(memberDto.getWechatCode(), "微信二维码", 3, 500);
        ValidatorUtil.requiredEmpty(memberDto.getMobile(), "手机号");
        ValidatorUtil.requiredLength(memberDto.getMobile(), "手机号", 3, 255);
        ValidatorUtil.requiredLength(memberDto.getEmail(), "邮箱", 3, 255);
        ValidatorUtil.requiredLength(memberDto.getPassword(), "密码", 3, 255);
        ValidatorUtil.requiredLength(memberDto.getIdentityNum(), "身份证号", 3, 255);
        ValidatorUtil.requiredLength(memberDto.getProvince(), "省", 3, 50);
        ValidatorUtil.requiredLength(memberDto.getCity(), "市", 3, 50);
        ValidatorUtil.requiredLength(memberDto.getDistrict(), "区", 3, 50);
        ValidatorUtil.requiredLength(memberDto.getBankNum(), "银行卡号", 3, 50);
        ValidatorUtil.requiredLength(memberDto.getBankCard(), "银行卡", 3, 500);
        ValidatorUtil.requiredLength(memberDto.getRegisterId(), "注册IP", 3, 50);
        ValidatorUtil.requiredLength(memberDto.getMotto(), "座右铭", 3, 255);*/
        //对密码进行后端二次加密
        memberDto.setPassword(DigestUtils.md5DigestAsHex(memberDto.getPassword().getBytes()));

        ResponseDataDto responseData = new ResponseDataDto();
        memberService.save(memberDto);
        responseData.setResponseData(memberDto);
        return responseData;
    }

    /**
     * 指定请求的格式为Delete
     * 4.删除模块，如果为多个参数，就定义多个/{param}/{param}
     */
    @DeleteMapping("/del/{uniId}")
    public ResponseDataDto del(@PathVariable String uniId) {
        LOG.info("传入的member uniId：{}", uniId);
        ResponseDataDto responseData = new ResponseDataDto();
        memberService.delete(uniId);
        return responseData;
    }

    //保存图片
    @PostMapping("/savePicList")
    public ResponseDataDto savePicList(@RequestBody ModulePicPathDto modulePicPathDto){
        ResponseDataDto responseData=new ResponseDataDto();
        responseData.setResponseData(memberService.savePicList(modulePicPathDto));
        return responseData;
    }
    //更新会员密码
    @PostMapping("/save-pwd")
    public ResponseDataDto saveMemberPwd(@RequestBody MemberDto memberDto){
        LOG.info("传入的密码：{}",memberDto.getPassword());
        memberDto.setPassword(DigestUtils.md5DigestAsHex(memberDto.getPassword().getBytes()));
        LOG.info("加密的密码：{}",memberDto.getPassword());
        ResponseDataDto responseData=new ResponseDataDto();
        MemberDto member= memberService.saveMemberPwd(memberDto);
        responseData.setResponseData(member);
        return responseData;
    }
}
